<?php

    class Article2 extends MY_Controller
    {
        public function __construct()
        {
            parent::__construct();
            $this->load->model('admin/article_model');
            $this->load->model('admin/category_article_model');
        }
        public function index()
        {
            $data=$this->article_model->getAll(10,0,-1);
            $this->load->vars('item',$data);
        }
        
        //add new article
        public function add()
        {
            $category=$this->category_article_model->getAll(10,0,1);
            $this->load->vars('category',$category);
        }
        
        //edit article 
        public function edit()
        {
            $id=$this->uri->segment(4);
            $category=$this->category_article_model->getAll(10,0,1);
            $this->load->vars('category',$category);
            $this->data['item']=$this->article_model->getarticleById($id);
            $this->load->vars('item',$this->data['item']);
        }
        
        //save new article or update 
        public function save()
        {
           if ($this->form_validation->run('article') == FALSE)
            {
                $this->form_validation->set_error_delimiters('<div class="error" style="text-align:center;">', '</div>');
                $this->load->view('admin/form/form');
            }
            else
            {
                $post=$this->input->post();
                $id=$post['id'];
                $name=$post['name'];
                $alias=$post['alias'];
                $category=$post['category'];
                $description=$post['desciption'];
                $publish=$post['publish'];
                $order=$post['order'];
                $data=array();
                $data['article_title']=$name;
                $data['article_alias']=$alias;
                $data['article_content']=$description;
                $data['category_article']=$category;
                $data['article_order']=$order;
                $data['article_publish']=$publish;
                if($id>0)
                {
                    if($this->input->post('image')!="")
                    {
                        $image=$this->doupload('image','articles');
                        $data['article_image_path']=$image;
                    }
                }
                else
                {
                    $image=$this->doupload('image','articles');
                    $data['article_image_path']=$image;
                }
                //call save
                if($this->article_model->save($id,$data)>0)
                {
                    
                    //redirect(base_url()."admin/article");
                }
                
            //}// End if formvalidation
            redirect(base_url()."admin/article2");
        }
    }
        //publish article
        public function publish()
        {
            $id=$this->uri->segment(4);
            $this->article_model->publish(1,array($id));
            redirect(base_url()."admin/article2");
        }
        //unpublish
        public function unpublish()
        {
            $id=$this->uri->segment(4);
            $this->article_model->publish(0,array($id));
            redirect(base_url()."admin/article2");
        }
        //for actions from menu
        
        public function action()
        {
            $task=$this->input->post('task');
            $task=trim($task);
            $id=$this->input->post('id');
            switch($task)
            {
                case 'saveorder':
                $cid=$this->input->post('cid');
                $order=$this->input->post('order');
                $this->article_model->saveorder($cid,$order);
                break;
                case 'delete':
                $this->article_model->delete($id);
                break;
                case 'publish':
                $this->article_model->publish(1,$id);
                break;
                case 'unpublish':
                $this->article_model->publish(0,$id);
                break;
                
            }
            redirect(base_url()."admin/article2");
        }
    }